package com.song.oa.filter;

import com.song.oa.util.WebUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

/**
 * 当未登录或者token失效时访问接口时，自定义的返回结果
 * @author 宋伟宁
 */
@Component
public class RestfulAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) {
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json");
//        PrintWriter out = response.getWriter();
//        RespBean bean = RespBean.error(("权限不足，请联系管理员"));
//        bean.setCode(403);
//        out.write(new ObjectMapper().writeValueAsString(bean));
//        out.flush();
//        out.close();
        WebUtils.renderString(response,"权限不足，请联系管理员");
    }
}
